package com.dd.census.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import com.dd.census.controller.common.BaseController;
import com.dd.census.emums.DictionaryGroupEnum;
import com.dd.census.entity.Dictionary;
import com.dd.census.service.DictionaryService;
import com.dd.census.tools.SysException;
import com.dd.census.tools.validator.ValidateUtil;

@Controller
@RequestMapping("rest/dictionary")
public class DictionaryController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(DictionaryController.class);

	@Autowired
	private DictionaryService dictionaryService;

	@RequestMapping("selectPage")
	public ResponseEntity<Object> select(@RequestBody Dictionary dictionary, HttpServletRequest request) {
		try {
			// 基本输入验证
			ValidateUtil.validate(dictionary);
			return this.doSuccess(request, dictionaryService.selectPage(dictionary));

		} catch (SysException e) {
			// 业务异常
			return doFaild(request, e);

		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return doFaild(request, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}

	@RequestMapping("selectAll")
	public ResponseEntity<Object> selectAll(@RequestBody Dictionary dictionary, HttpServletRequest request) {
		try {
			// 基本输入验证
			ValidateUtil.validate(dictionary);

			return this.doSuccess(request, dictionaryService.selectAll(dictionary));

		} catch (SysException e) {
			// 业务异常
			return doFaild(request, e);

		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return doFaild(request, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}

	@RequestMapping("/{id}")
	public ResponseEntity<Object> getById(@PathVariable("id") Integer id, HttpServletRequest request) {
		try {
			return this.doSuccess(request, dictionaryService.selectById(id));
		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return doFaild(request, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}

	@RequestMapping("selectGroup")
	public ResponseEntity<Object> selectGroup(@RequestBody Dictionary dictionary, HttpServletRequest request) {
		try {
			// 基本输入验证
			ValidateUtil.validate(dictionary);
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("items", DictionaryGroupEnum.toList());

			return this.doSuccess(request, map);

		} catch (SysException e) {
			// 业务异常
			return doFaild(request, e);

		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return doFaild(request, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}

	@RequestMapping("insert")
	public ResponseEntity<Object> insert(HttpServletRequest request, @RequestBody Dictionary dictionary) {

		try {
			// 基本输入验证
			ValidateUtil.validate(dictionary);
			dictionary.setCreateUserId(this.getUserId(request));
			dictionary.setCreateTime(new Date());
			// 插入数据
			List<Dictionary> list = dictionaryService.selectCode(dictionary.getGroupName());
			int next = 0;
			for(int i = 0; i<list.size(); i++){
				if(dictionary.getCode().equals(list.get(i).getCode())){
					next+=i;
				}
			}
			
	    if(next == 0){
	    	dictionaryService.insert(dictionary);
	       }else{
	    	   
	       }

			return this.doSuccess(request,next);
		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return doFaild(request, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}

	@RequestMapping("update")
	public ResponseEntity<Object> update(HttpServletRequest request, @RequestBody Dictionary dictionary) {

		try {
			// 基本输入验证
			ValidateUtil.validate(dictionary);
			dictionary.setUpdateUserId(this.getUserId(request));
			dictionary.setUpdateTime(new Date());
			dictionaryService.update(dictionary);

			return this.doSuccess(request);
		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return doFaild(request, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
	
	@RequestMapping("delete")
	public ResponseEntity<Object> delete(HttpServletRequest request, @RequestBody Dictionary dictionary) {

		try {
			// 基本输入验证
			ValidateUtil.validate(dictionary);
			dictionaryService.delete(dictionary);
			return this.doSuccess(request);
		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return doFaild(request, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
	
	@RequestMapping("getDictionarys")
	public ResponseEntity<Object> getDictionary(HttpServletRequest request,String groupName) {
		
        try {
        	Dictionary dictionary = new Dictionary();
    		dictionary.setGroupName(groupName);
            List<Dictionary> list =dictionaryService.selectAll(dictionary);
			return this.doSuccess(request, list);

		} catch (SysException e) {
			// 业务异常
			return doFaild(request, e);

		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return doFaild(request, HttpStatus.INTERNAL_SERVER_ERROR);
		}
    }
}